package com.example.demo.exception

import com.example.demo.common.BaseResponse
import com.example.demo.common.ResultUtils
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.RestController

@ControllerAdvice(annotations = [RestController::class])
@ResponseBody
class GlobalExceptionHandler {

    /**
     * 处理业务异常
     */

    @ExceptionHandler(BusinessException::class)
    fun handleBusinessException(e: BusinessException): BaseResponse<Any> {
        return ResultUtils.error(ErrorCode.INTERNAL_SERVER_ERROR, e.message?:ErrorCode.INTERNAL_SERVER_ERROR.message)
    }

    /**
     * 处理运行异常
     */
    @ExceptionHandler(value = [RuntimeException::class])
    fun handleRuntimeException(e: RuntimeException): BaseResponse<Any> {
        return ResultUtils.error(ErrorCode.INTERNAL_SERVER_ERROR,e.message?:ErrorCode.INTERNAL_SERVER_ERROR.message)
    }
}